package com.ulook.lookplanet.service;

import com.ulook.lookplanet.bean.TSysMsg;
import com.ulook.lookplanet.bean.TUserMsg;
import com.ulook.lookplanet.bean.VMsgUser;
import com.ulook.lookplanet.mapper.NotificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/4/29
 */
@Service
public class NotificationService {

    @Autowired
    private NotificationMapper mapper;

    /**
     * 查询系统消息
     */
    @Transactional
    public List<TSysMsg> querySysMsg(int userid){
        List<TSysMsg> resList = mapper.querySysMsg(userid);
        int resNum = 0;
        for(int i = 0; i < resList.size(); i++){
            resNum = resNum +  mapper.updateSysMsg(resList.get(i).getUid());
        }
        /*if(resNum != resList.size()){
            return null;
        }*/
        return resList;
    }

    /**
     * 查询用户消息
     */
    @Transactional
    public List<VMsgUser> queryUserMsg(int touser){
        List<VMsgUser> resList = mapper.queryUserMsg(touser);
        int resNum = 0;
        for(int i = 0; i < resList.size(); i++){
            System.out.println(resList.get(i).toString());
            resNum = resNum +  mapper.updateUserMsg(resList.get(i).getUid());
        }
        /*if(resNum != resList.size()){
            return null;
        }*/
        return resList;
    }

    /**
     * 发送消息
     */
    @Transactional
    public void addUserMsg(TUserMsg userMsg){
        mapper.addUserMsg(userMsg);
    }
}
